Inside Macintosh: Imaging with QuickDraw

Previous | Chapter Top | Chapter Contents | Next

Opening and Closing Color Graphics Ports

All graphics operations are performed in graphics ports. Before a color graphics port can be used, it must be allocated with the OpenCPort procedure and initialized with the InitCPort procedure. Normally, your application does not call these procedures directly. Instead, your application creates a graphics port by using the GetNewCWindow or NewCWindow function (described in the chapter "Window Manager" in Inside Macintosh: Macintosh Toolbox Essentials ) or the NewGWorld function (described in the chapter "Offscreen Graphics Worlds" in this book). These functions automatically call OpenCPort , which in turn calls InitCPort .

To dispose of a color graphics port when you are finished using a color window, you normally use the DisposeWindow procedure (if you let the Window Manager allocate memory for the window) or the CloseWindow procedure (if you allocated memory for the window). You use the DisposeGWorld procedure when you are finished with a color graphics port for an offscreen graphics world. These routines automatically call the CloseCPort procedure. If you use the CloseWindow procedure, you also dispose of the window record containing the graphics port by calling the Memory Manager procedure DisposePtr .

OpenCPort

The OpenCPort procedure allocates space for and initializes a color graphics port. The Window Manager calls OpenCPort for every color window that it creates, and the NewGWorld procedure calls OpenCPort for every offscreen graphics world that it creates on a Color QuickDraw computer.

PROCEDURE OpenCPort (port: CGrafPtr);
port
A pointer to a CGrafPort record.

DESCRIPTION

The OpenCPort procedure is analogous to OpenPort (described in the chapter "Basic QuickDraw"), except that OpenCPort opens a CGrafPort record instead of a GrafPort record. The OpenCPort procedure is called by the Window Manager's NewCWindow and GetNewCWindow procedures, as well as by the Dialog Manager when the appropriate color resources are present. The OpenCPort procedure allocates storage for all the structures in the CGrafPort record, and then calls InitCPort to initialize them. The InitCPort procedure does not allocate a color table for the PixMap record for the color graphics port; instead, InitCPort copies the handle to the current device's CLUT into the PixMap record. The initial values for the CGrafPort record are shown in Table 4-3 .

Table 3 Initial values in the CGrafPort record 

Field

Data type

Initial setting

device Integer

0 (the screen)

portPixMap PixMapHandle

Handle to the port's PixMap record

portVersion Integer

$C000

grafVars Handle

Handle to a GrafVars record where black is assigned to the rgbOpColor field, the default highlight color is assigned to the rgbHiliteColor field, and all other fields are set to 0

chExtra Integer

0

pnLocHFrac Integer

The value in this field represents the low word of a Fixed number; in decimal, its initial value is 0.5.

portRect Rect

screenBits.bounds (boundary for entire main screen)

visRgn RgnHandle

Handle to a rectangular region coincident with screenBits.bounds

clipRgn RgnHandle

Handle to the rectangular region (-32768,-32768,32767,32767)

bkPixPat Pattern

White

rgbFgColor RGBColor Black
rgbBkColor RGBColor

White

pnLoc Point

(0,0)

pnSize Point

(1,1)

pnMode Integer patCopy
pnPixPat PixPatHandle

Black

fillPixPat PixPatHandle

Black

pnVis Integer

0 (visible)

txFont Integer

0 (system font)

txFace Style

Plain

txMode Integer srcOr
txSize Integer

0 (system font size)

spExtra Fixed

0

fgColor LongInt blackColor
bkColor LongInt whiteColor
colrBit Integer

0

patStretch Integer

0

picSave Handle NIL
rgnSave Handle NIL
polySave Handle NIL
grafProcs CQDProcsPtr NIL

The additional structures allocated are the portPixMap , pnPixPat , fillPixPat , bkPixPat , and grafVars handles, as well as the fields of the GrafVars record.

SPECIAL CONSIDERATIONS

The OpenCPort procedure may move or purge memory blocks in the application heap. Your application should not call this procedure at interrupt time.

InitCPort

The OpenCPort procedure uses the InitCPort procedure to initialize a color graphics port.

PROCEDURE InitCPort (port: CGrafPtr);
port
A pointer to a CGrafPort record.

DESCRIPTION

The InitCPort procedure is analogous to InitPort (described in the chapter "Basic QuickDraw"), except InitCPort initializes a CGrafPort record instead of a GrafPort record. The InitCPort procedure does not allocate any storage; it merely initializes all the fields in the CGrafPort and GrafVars records to the default values shown in Table 4-3 .

The PixMap record for the new color graphics port is set to be the same as the current device's PixMap record. This allows you to create an offscreen graphics world that is identical to the screen's port for drawing offscreen. If you want to use a different set of colors for offscreen drawing, you should create a new GDevice record and set it as the current GDevice record before opening the CGrafPort record.

Remember that InitCPort does not copy the data from the current device's CLUT to the color table for the graphics port's PixMap record. It simply replaces whatever is in the PixMap record's pmTable field with a copy of the handle to the current device's CLUT.

If you try to initialize a GrafPort record using InitCPort , it simply returns without doing anything.

SPECIAL CONSIDERATIONS

The InitCPort procedure may move or purge memory blocks in the application heap. Your application should not call this procedure at interrupt time.

SEE ALSO

The chapter "Graphics Devices" in this book describes GDevice records; the chapter "Offscreen Graphics Worlds" in this book describes how to use offscreen graphics worlds.

CloseCPort

The CloseCPort procedure closes a color graphics port. The Window Manager calls this procedure when you close or dispose of a window, and the DisposeGWorld procedure calls it when you dispose of an offscreen graphics world containing a color graphics port.

PROCEDURE CloseCPort (port: CGrafPtr);
port
A pointer to a CGrafPort record.

DESCRIPTION

The CloseCPort procedure releases the memory allocated to the CGrafPort record. It disposes of the visRgn , clipRgn , bkPixPat , pnPixPat , fillPixPat , and grafVars handles. It also disposes of the graphics port's pixel map, but it doesn't dispose of the pixel map's color table (which is really owned by the GDevice record). If you have placed your own color table into the pixel map, either dispose of it before calling CloseCPort or store another reference.

SPECIAL CONSIDERATIONS

The CloseCPort procedure may move or purge memory blocks in the application heap. Your application should not call this procedure at interrupt time.


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next